﻿/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#include "../import/head.h"
#include "../impl/head.h"
#include "list_iter.h"

/////////////////////////////////////////////////////////////////////////////////////////
// 
namespace xos_box
{
    ////////////////////////////////////////////////////////////////////////////////////
    // 

    static list_iter::POOL * pool_ptr = 0;

    ////////////////////////////////////////////////////////////////////////////////////

    list_iter::list_iter()
    {
        m_bForward = true;
        m_pList = 0;
        init_data();
    }

    list_iter::~list_iter()
    {
        term();
    }

    ////////////////////////////////////////////////////////////////////////////////////
    // 

    int list_iter::get_addr( POOL *** pppPool, LIST *** pppList )
    {
        int ret = 0;
        if( pppPool )
        {
            *pppPool = &pool_ptr;
        }
        if( pppList )
        {
            *pppList = 0;
        }
        return ret;
    }

    int list_iter::set( xos_list::ITEM_LIST * pList, bool bForward )
    {
        int ret = 0;
        m_bForward = bForward;
        m_pList = pList;
        init_data();
        return ret;
    }

    int list_iter::init_data()
    {
        int ret = 0;
        if( m_pList )
        {
            m_r_iter = m_pList->rbegin();
            m_r_end = m_pList->rend();
            m_iter = m_pList->begin();
            m_end = m_pList->end();
        }
        return ret;
    }

    int list_iter::init()
    {
        int ret = 0;
        return ret;
    }

    int list_iter::term()
    {
        int ret = 0;
        init_data();
        return ret;
    }

    ////////////////////////////////////////////////////////////////////////////////////
    // 

    xos::i_release * list_iter::next()
    {
        xos::i_release * pRet = 0;
        if( !m_pList )
        {
            return 0;
        }
        if( m_bForward )
        {
            if( m_iter != m_end )
            {
                pRet = *m_iter++;
            }
        }
        else
        {
            if( m_r_iter != m_r_end )
            {
                pRet = *m_r_iter++;
            }
        }
        return pRet;
    }

} // xos
